package com.wxm.killing.mq.common.util;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @author: wxm
 * @date: 2019/10/27 22:33
 * @version: 1.0
 */
public class CommonUtil {

    public static String createContractRushOrderSn(Integer id) {
        String start = DateUtil.getNowString(DateUtil.YYYYMMddHHMMSS);
        String end = "100000" + id.toString();
        return "RO" + start + end;
    }

    public static String createContractRushBuySn(Integer id) {
        String start = DateUtil.getNowString(DateUtil.YYYYMMddHHMMSS);
        String end = "100000" + id.toString();
        return "RB" + start + end;
    }

    /**
     * 获取字符串
     *
     * @param map
     * @param key
     * @return
     */
    public static String getString(Map<String, Object> map, String key) {
        String value = "";
        if (map.containsKey(key)) {
            Object o = map.get(key);
            if (o != null) {
                value = o.toString();
            }
        }
        return value;
    }

    /**
     * 获取integer值
     *
     * @param map
     * @param key
     * @return
     */
    public static Integer getInteger(Map<String, Object> map, String key) {
        Integer in = null;
        Object object = map.get(key);
        if (object != null) {
            if (object instanceof Integer) {
                in = (Integer) object;
            } else if (object instanceof String) {
                in = Integer.parseInt((String) object);
            } else if (object instanceof Double) {
                in = (int) ((double) object);
            } else if (object instanceof Float) {
                in = (int) ((float) object);
            } else if (object instanceof BigDecimal) {
                in = ((BigDecimal) object).intValue();
            } else if (object instanceof Long) {
                in = ((Long) object).intValue();
            }
        }
        return in;
    }

    /**
     * 获取字符串
     *
     * @param map
     * @param key
     * @return
     */
    public static BigDecimal getBigDecimal(Map<String, Object> map, String key) {
        BigDecimal bigDecimal = new BigDecimal(0);
        if (map.containsKey(key)) {

        }
        return bigDecimal;
    }

    public static String getRealIp(HttpServletRequest request) {
        // 这个一般是Nginx反向代理设置的参数
        String ip = request.getHeader("X-Real-IP");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        // 处理多IP的情况（只取第一个IP）
        if (ip != null && ip.contains(",")) {
            String[] ipArray = ip.split(",");
            ip = ipArray[0];
        }
        return ip;
    }

}
